package org.vectrics.recruiter.reporting;

import java.awt.Color;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfRectangle;
import com.lowagie.text.Image;
import org.apache.log4j.Logger;
import org.vectrics.VectricsException;
import org.vectrics.common.reporting.pdf.ReportUtil;
import org.vectrics.hibernate.ThreadProperties;

import java.util.Iterator;
import java.util.Locale;
import java.util.TreeSet;

import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.skill.SkillChildComparator;
import org.vectrics.skill.SkillSetUtil;
import org.vectrics.skill.Skill;
import org.vectrics.skill.SkillRatingSet;
import org.vectrics.skill.SkillRating;
import org.vectrics.struts.PhraseManager;



public class SkillRatingPdfTable extends PdfPTable {
	private transient static Logger log = Logger.getLogger(SkillRatingPdfTable.class);
	private boolean showAllBranches = false;
	private SkillSetUtil skillSetUtil = null;
	private String caption = null;
	private static int numberOfColumns = 2;
	private Locale locale = null;
	
	public SkillRatingPdfTable(String captionCode, Locale locale) {
		super(numberOfColumns);
		this.locale = locale;
		this.caption = CoreServiceLocator.getPhraseProvider().getMessage(captionCode, locale);
		this.setWidthPercentage(100);
	}
	
	
	protected String getPhrase(String code) {
		return CoreServiceLocator.getPhraseProvider().getMessage(code, this.locale);
	}
	
	public void initialize(Skill parentSkill, 
			SkillRatingSet ratingSet, int level) throws VectricsException {
		if (this.caption != null) {
			PdfPCell hdrCell = new PdfPCell(new Phrase(this.caption, 
							FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD)));
			hdrCell.setPaddingBottom(3);
			hdrCell.setColspan(numberOfColumns);
			hdrCell.setBackgroundColor(ReportConstants.TABLE_LABEL_COLOR);
			this.addCell(hdrCell);
		}
		
		this.skillSetUtil = new SkillSetUtil(ratingSet, parentSkill);
		displaySkillBranch(this, parentSkill, ratingSet, level);
			
	}
		
	
	protected void displaySkill(PdfPTable table, Skill parentSkill, 
			SkillRatingSet ratingSet, int level) throws VectricsException {
		try {
			log.debug("addJobRow() - displaying rating");
			String ratingDesc = "";
			SkillRating rating = ratingSet.findSkillRating(parentSkill);
			
			
			
			
			PdfRectangle rect = new PdfRectangle(10, 20);
			
			Color color = new Color(255, 255, 255);
			
			log.debug("addJobRow() - displaying date...");
			PdfPTable descTable = new PdfPTable(3);
			int descWidths[] = {level * 10, 10, 200};
			descTable.setWidthPercentage(100);
			descTable.setWidths(descWidths);
			PdfPCell cell1 = new PdfPCell(ReportUtil.createPhrase(" ", ReportConstants.smlFont));
			descTable.addCell(cell1);			
			cell1.setBorderWidth(0);
			
			PdfPCell cell2 = new PdfPCell(ReportUtil.createPhrase(parentSkill.getDescription(), ReportConstants.smlFont));
			cell2.setBorderWidth(0);
			descTable.addCell(cell2); 
			
			PdfPCell cell = new PdfPCell(descTable);
			cell.setBackgroundColor(color);
			if (ratingDesc.length() <= 0)
				ratingDesc = " ";
			
			
			
			PdfPCell ratingCell = null;
			if ((rating == null) || (rating.hasSkill() == false)) {
				ratingCell = new PdfPCell(ReportUtil.createPhrase(" ", ReportConstants.smlFont));
			}
			else {
				PdfPTable ratingTable = new PdfPTable(2);
				int ratingWidths[] = {20, 80};
				ratingTable.setWidthPercentage(100);
				ratingTable.setWidths(ratingWidths);
				ratingDesc = this.getPhrase(rating.getDescription());
				String lvlStr = "";
				if (rating.getLevel() != null) {
					lvlStr += " " + rating.getLevel();
				}
				if (lvlStr.length() <= 0) {
					lvlStr = " "; 
				}
				Font levelFont = ReportConstants.copyFont(ReportConstants.smlBoldFont);
				levelFont.setColor(new Color(20, 20, 200));
					
				PdfPCell ratingLevelCell = new PdfPCell(ReportUtil.createPhrase(lvlStr, levelFont));
				ratingTable.addCell(ratingLevelCell);
				PdfPCell ratingDescCell = new PdfPCell(ReportUtil.createPhrase(ratingDesc, ReportConstants.smlFont));
				ratingTable.addCell(ratingDescCell);
				
				ratingCell = new PdfPCell(ratingTable);
			}
			
			
			ratingCell.setBackgroundColor(color);
			table.addCell(cell);	
			table.addCell(ratingCell);	
		}
		catch (Exception ex) {
			log.error("Exception caught while adding skill row = " + parentSkill.getCode(), ex);
			throw new VectricsException("Exception caught while adding job row for job = " + parentSkill.getCode(), ex);
		}	
	}   
	
	
	
	void displaySkillBranch(PdfPTable table, Skill parentSkill, SkillRatingSet ratingSet, int level) throws VectricsException {
		displaySkill(table, parentSkill, ratingSet, level);
		TreeSet children = new TreeSet(new SkillChildComparator());
		children.addAll(parentSkill.getChildren());
		Iterator iter =  children.iterator();
		if ((this.getShowAllBranches()) || (this.skillSetUtil.hasSkillRatingInBranch(parentSkill))) {
			while (iter.hasNext()) {
				Skill child = (Skill)iter.next();
				displaySkillBranch(table, child, ratingSet, level + 1);
			}
		}
	}
	
	/**
	 * @return Returns the showAllBranches.
	 */
	public boolean getShowAllBranches() {
		return showAllBranches;
	}

	/**
	 * @param showAllBranches The showAllBranches to set.
	 */
	public void setShowAllBranches(boolean showAllBranches) {
		this.showAllBranches = showAllBranches;
	}

}
